package models

import (
	"fmt"
	"rongyy/src/app"
)

type DoctorModel struct {
	BaseModel `xorm:"extends"`
	Sn        string `json:"sn"`
	Descript  string `json:"descript"`  // 医生简介
	Image     string `json:"image"`     // 头像图片
	NewImage  string `json:"new_image"` // 新头像图片
	RealName  string `json:"real_name"`
	Del       int    `json:"del"`
	OldSn     string `json:"old_sn"`
}

type NewOldCompare struct {
	NewCode string `json:"new_code"`
	OldCode string `json:"old_code"`
}

func (pm *DoctorModel) TableName() string {
	return "sys_employee"
}

func (rm *DoctorModel) GetDoctorByIdM(sn string) (*DoctorModel, bool, error) {
	doc := &DoctorModel{}

	has, err := app.DB().Table("sys_employee").Where("sn = ? and del = 0", sn).Get(doc)
	if err != nil {
		return nil, false, err
	}
	return doc, has, err
}

func (rm *DoctorModel) GetDoctorForReg(sn string) (*DoctorModel, bool, error) {
	doc := &DoctorModel{}
	//timeline, err := time.ParseInLocation("2006-01-02 15:04:05", "2024-04-01 00:00:00", time.Local)
	//fmt.Println(timeline)
	//has, err := app.DB().Table("sys_employee").Where("sn = ? and del = 0 and updated_at>?", sn, timeline).Get(doc)
	////t, err := time.ParseInLocation("2006-01-02 15:04:05", doc.UpdatedAt.String(), time.Local)
	//if has {
	//	return doc, has, err
	//}
	//noc := &NewOldCompare{}
	//zhas, err := app.DB().SQL("SELECT dn.CTPCP_RowId1 as new_code, dol.CTPCP_RowId1 as old_code from doctor_list_his_new dn left join doctor_list_his_old dol on dn.CTPCP_Code = dol.CTPCP_Code where dn.CTPCP_RowId1 = ?", sn).Get(noc)
	//if !zhas {
	//	return nil, false, err
	//}
	//fmt.Println(noc)
	has, err := app.DB().Table("sys_employee").Where("sn = ? and del = 0", sn).Get(doc)
	if err != nil {
		return nil, false, err
	}
	return doc, has, err
}

func (rm *DoctorModel) Query(limit int, offset int, name string) ([]DoctorModel, int64, error) {
	doc := make([]DoctorModel, 0)
	s := app.DB().Table("sys_employee").Where("del = 0")
	if name != "" {
		name1 := fmt.Sprintf("%%%s%%", name)
		s.Where("real_name like ?", name1)
	}
	total, err := s.Limit(limit, offset).FindAndCount(&doc)
	if err != nil {
		return nil, 0, err
	}
	return doc, total, err
}

func (rm *DoctorModel) QueryBatch(limit int, offset int, hospital string) ([]DoctorModel, int64, error) {
	doc := make([]DoctorModel, 0)
	total, err := app.DB().Table("sys_employee").Where("del = 0  and image !='' and new_image = ''").Limit(limit, offset).FindAndCount(&doc)
	if err != nil {
		return nil, 0, err
	}
	return doc, total, err
}

func (rm *DoctorModel) Update() error {
	_, err := app.DB().Table("sys_employee").Where("sn = ? and del = 0", rm.Sn).Update(rm)
	if err != nil {
		return err
	}
	return err
}

func (rm *DoctorModel) Delete() error {
	rm.Del = 1
	_, err := app.DB().Table("sys_employee").Where("sn = ? and del = 0", rm.Sn).Update(rm)
	if err != nil {
		return err
	}
	return err
}

func (rm *DoctorModel) Create() error {
	_, err := app.DB().Table("sys_employee").Insert(rm)
	if err != nil {
		return err
	}
	return err
}

func (rm *DoctorModel) UpdateSn() error {
	doc := make([]DoctorModel, 0)
	_, _ = app.DB().Table("sys_employee").Limit(999, 0).FindAndCount(&doc)
	for _, item := range doc {
		sn := item.OldSn
		noc := &NewOldCompare{}
		zhas, _ := app.DB().SQL("SELECT dn.CTPCP_RowId1 as new_code, dol.CTPCP_RowId1 as old_code from doctor_list_his_new dn left join doctor_list_his_old dol on dn.CTPCP_Code = dol.CTPCP_Code where dol.CTPCP_RowId1  = ?", sn).Get(noc)
		fmt.Println(zhas)
		if zhas {
			fmt.Println(noc)
			rm := &DoctorModel{}
			rm.Id = item.Id
			rm.OldSn = noc.OldCode
			rm.Sn = noc.NewCode
			_, _ = app.DB().Table("sys_employee").Where("old_sn = ? and del = 0", rm.OldSn).Cols("sn").Update(rm)
		}
	}
	return nil
}
